﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;

namespace Ko2.Portal
{

    public static class SPSecurityHelper
    {

        #region Group
        public static bool ExistsGroupWeb(string parSiteUrl, String mName)
        {
            Boolean mRet = false;
            using (SPSite Site = new SPSite(parSiteUrl))
            {
                mRet = ExistsGroupWeb(Site, mName);
            }
            return mRet;
        }
        public static bool ExistsGroupWeb(SPSite Site, String mName)
        {
            Boolean mRet = false;
            using (SPWeb mWeb = Site.OpenWeb())
            {
                mRet = ExistsGroupWeb(mWeb, mName);
            }
            return mRet;
        }
        public static bool ExistsGroupWeb(SPWeb mWeb, String mName)
        {
            Boolean mRet = false;
            foreach (SPGroup mGroup in mWeb.SiteGroups)
            {
                if (mGroup.Name == mName)
                {
                    mRet = true;
                    break;
                }
            }
            return mRet;
        }
        public static SPGroup GetGroup(string parSiteUrl, String mName)
        {
            SPGroup ret = null;
            using (SPSite Site = new SPSite(parSiteUrl))
            {
                ret = GetGroup(Site, mName);
            }
            return ret;
        }
        public static SPGroup GetGroup(SPSite Site, String mName)
        {
            SPGroup ret = null;
            using (SPWeb mWeb = Site.OpenWeb())
            {
                ret = GetGroup(mWeb, mName);
            }
            return ret;
        }
        public static SPGroup GetGroup(SPWeb mWeb, String mName)
        {
            SPGroup ret = null;
            if (ExistsGroupWeb(mWeb, mName))
                ret = mWeb.SiteGroups[mName];
            return ret;
        }
        public static string GetNameGroupByString(string GroupValue)
        {
            string Ret = null;
            if (GroupValue != null)
            {
                int mPos = GroupValue.IndexOf(";#");
                if (mPos > 0)
                {
                    Ret = GroupValue.Substring(GroupValue.IndexOf(";#") + 2);  // GroupValue.Length - 2 - GroupValue.IndexOf(";#")
                    Ret = Ret.Trim();
                }
            }
            return Ret;
        }
        public static bool GetGroupByString(string GroupValue, ref int retId, ref string retName)
        {
            bool Ret = false;
            if ( !String.IsNullOrEmpty(GroupValue))
            {
                string [] separator = {";#"};
                string [] arr = GroupValue.Split(separator, StringSplitOptions.RemoveEmptyEntries);

                if (arr.Length >= 2)
                {
                    retId = Convert.ToInt32(arr[0]);
                    retName = arr[1];
                    Ret = true;
                }
            }
            return Ret;
        }
        #endregion Group

        #region Users
        public enum SPUserNameType { Name=1, Login=2, EMail=3};
        public static SPUser GetUserByName(SPWeb web, string userName, SPUserNameType nameType)
        {
            if (!string.IsNullOrEmpty(userName))
            {
                userName = userName.ToLower();
                foreach (SPUser user in web.SiteUsers)
                {
                    if (nameType == SPUserNameType.Name)
                    {
                        if (userName == user.Name.ToLower())
                            return user;
                    }
                    else if (nameType == SPUserNameType.Login)
                    {
                        if (userName == user.LoginName.ToLower())
                            return user;
                    }
                    else if (nameType == SPUserNameType.EMail)
                    {
                        if (userName == user.Email.ToLower())
                            return user;
                    }
                }
            }
            return null;
        }
        public static void SetUserPermission(SPListItem item, string userName, string roleName, SPUserNameType nameType)
        {
            SPUser addedUser = GetUserByName(item.Web, userName, nameType);
            if (addedUser != null)
            {
                SPRoleAssignment userAssignment = new SPRoleAssignment(addedUser);
                userAssignment.RoleDefinitionBindings.Add(item.Web.RoleDefinitions[roleName]);
                item.RoleAssignments.Add(userAssignment);
            }
        }
        public static void SetUserPermission(SPListItem item, SPUser addedUser, string roleName)
        {
            if (addedUser != null)
            {
                try
                {
                    SPRoleAssignment userAssignment = new SPRoleAssignment(addedUser);
                    userAssignment.RoleDefinitionBindings.Add(item.Web.RoleDefinitions[roleName]);
                    item.RoleAssignments.Add(userAssignment);
                }
                catch
                {
                }
            }
        }
        #endregion

    }

}
